<template>
  <div class="module-card">
    <div class="module-header">
      <button @click="showAddMeetingDialog = true" class="btn-primary">新增会议内容</button>
    </div>

    <!-- 列表和详情视图 -->
    <div class="meeting-container">
      <!-- 会议列表 -->
      <div class="meeting-list-container" :class="{ 'list-full': !selectedMeeting }">
        <div class="meeting-list">
          <div 
            v-for="meeting in paginatedMeetings" 
            :key="meeting.meetingId" 
            class="meeting-item"
            :class="{ 'active': selectedMeeting && selectedMeeting.meetingId === meeting.meetingId }"
            @click="selectMeeting(meeting)"
          >
            <div class="meeting-header">
              <h4>{{ meeting.title }}</h4>
              <div class="meeting-meta">
                <span>日期：{{ formatDisplayDate(meeting.date) }}</span>
                <span>主持人：{{ meeting.host }}</span>
              </div>
            </div>
            <div class="meeting-content">
              <p>{{ meeting.content.length > 100 ? meeting.content.slice(0, 100) + '...' : meeting.content }}</p>
            </div>
          </div>
          <div v-if="meetings.length === 0" class="no-data">暂无会议记录</div>
        </div>

        <!-- 分页 -->
        <div class="pagination" v-if="meetings.length > 0">
          <button 
            @click="currentPage > 1 && (currentPage--)" 
            :disabled="currentPage === 1"
            class="btn-pagination"
          >
            上一页
          </button>
          <span class="page-info">
            第 {{ currentPage }} 页，共 {{ totalPages }} 页
          </span>
          <button 
            @click="currentPage < totalPages && (currentPage++)" 
            :disabled="currentPage === totalPages"
            class="btn-pagination"
          >
            下一页
          </button>
        </div>
      </div>

      <!-- 会议详情 -->
      <div v-if="selectedMeeting" class="meeting-detail">
        <div class="detail-header">
          <h3>{{ selectedMeeting.title }}</h3>
          <button @click="selectedMeeting = null" class="btn-close">×</button>
        </div>
        <div class="detail-meta">
          <p><strong>会议时间：</strong>{{ formatDisplayDate(selectedMeeting.date) }}</p>
          <p><strong>主持人：</strong>{{ selectedMeeting.host }}</p>
          <p><strong>参会人员：</strong>{{ selectedMeeting.participants }}</p>
          <p><strong>会议地点：</strong>{{ selectedMeeting.location || '未指定' }}</p>
        </div>
        <div class="detail-content">
          <h4>会议内容：</h4>
          <p>{{ selectedMeeting.content }}</p>
        </div>
        <div class="detail-actions">
          <button @click="handleEditMeeting(selectedMeeting)" class="btn-edit">编辑</button>
          <button @click="handleDeleteMeeting(selectedMeeting.meetingId)" class="btn-delete">删除</button>
        </div>
      </div>
    </div>

    <!-- 添加/编辑会议弹窗 -->
    <div v-if="showAddMeetingDialog" class="dialog-overlay">
      <div class="dialog">
        <div class="dialog-header">
          <h3>{{ editingMeeting ? '编辑会议' : '新增会议' }}</h3>
          <button @click="closeMeetingDialog" class="dialog-close">&times;</button>
        </div>
        <div class="dialog-content">
          <div class="form-group">
            <label>会议标题：</label>
            <input v-model="meetingForm.title" required>
          </div>
          <div class="form-group">
            <label>会议日期：</label>
            <input v-model="meetingForm.date" type="datetime-local" required>
          </div>
          <div class="form-group">
            <label>主持人：</label>
            <input v-model="meetingForm.host" required>
          </div>
          <div class="form-group">
            <label>参会人员：</label>
            <input v-model="meetingForm.participants" required>
          </div>
          <div class="form-group">
            <label>会议地点：</label>
            <input v-model="meetingForm.location">
          </div>
          <div class="form-group">
            <label>会议内容：</label>
            <textarea v-model="meetingForm.content" rows="5" required></textarea>
          </div>
        </div>
        <div class="dialog-footer">
          <button @click="saveMeeting" class="btn-primary">
            {{ editingMeeting ? '更新' : '保存' }}
          </button>
          <button @click="closeMeetingDialog" class="btn-secondary">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'

const meetings = ref([])
const showAddMeetingDialog = ref(false)
const editingMeeting = ref(null)
const selectedMeeting = ref(null)
const currentPage = ref(1)
const pageSize = ref(5)

// 表单字段匹配后端实体
const meetingForm = reactive({
  title: '',
  date: '',
  host: '',
  participants: '',
  location: '',
  content: '',
  departmentId: 1 // 固定部门ID
})

// 计算分页数据
const totalPages = computed(() => {
  return Math.ceil(meetings.value.length / pageSize.value)
})

const paginatedMeetings = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return meetings.value.slice(start, end)
})

// 获取会议列表
const fetchMeetings = async () => {
  try {
    const response = await fetch('http://localhost:8080/meeting/department/1')
    const result = await response.json()
    if (result.code === 200) {
      meetings.value = result.data
    }
  } catch (error) {
    console.error('获取会议列表失败:', error)
  }
}

// 选择会议查看详情
const selectMeeting = (meeting) => {
  selectedMeeting.value = meeting
}

// 优化日期显示格式
const formatDisplayDate = (dateStr) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 适配后端日期格式要求
const formatSubmitDate = (dateStr) => {
  if (!dateStr) return ''
  return new Date(dateStr).toISOString()
}

// 保存会议
const saveMeeting = async () => {
  try {
    const url = editingMeeting.value 
      ? 'http://localhost:8080/meeting/update' 
      : 'http://localhost:8080/meeting/add'
      
    const method = editingMeeting.value ? 'PUT' : 'POST'
    
    // 构建提交数据
    const submitData = {
      ...meetingForm,
      ...(editingMeeting.value && { meetingId: editingMeeting.value.meetingId }),
      date: formatSubmitDate(meetingForm.date)
    }
    
    const response = await fetch(url, {
      method: method,
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(submitData)
    })
    
    const result = await response.json()
    if (result.code === 200) {
      closeMeetingDialog()
      fetchMeetings()
      // 如果正在查看的会议被更新，也需要更新详情
      if (selectedMeeting.value && editingMeeting.value && 
          selectedMeeting.value.meetingId === editingMeeting.value.meetingId) {
        Object.assign(selectedMeeting.value, submitData)
      }
    } else {
      alert(result.msg || '操作失败')
    }
  } catch (error) {
    console.error('保存会议失败:', error)
    alert('保存失败，请重试')
  }
}

// 编辑会议
const handleEditMeeting = (meeting) => {
  editingMeeting.value = meeting
  const localDate = new Date(meeting.date).toISOString().slice(0, 16)
  
  Object.assign(meetingForm, {
    title: meeting.title,
    date: localDate,
    host: meeting.host,
    participants: meeting.participants,
    location: meeting.location || '',
    content: meeting.content
  })
  showAddMeetingDialog.value = true
}

// 删除会议
const handleDeleteMeeting = async (meetingId) => {
  if (!confirm('确定要删除该会议记录吗？')) return
  
  try {
    const response = await fetch(`http://localhost:8080/meeting/${meetingId}`, {
      method: 'DELETE'
    })
    
    const result = await response.json()
    if (result.code === 200) {
      fetchMeetings()
      // 如果正在查看的会议被删除，则关闭详情视图
      if (selectedMeeting.value && selectedMeeting.value.meetingId === meetingId) {
        selectedMeeting.value = null
      }
      // 重置当前页，避免出现空页
      if (paginatedMeetings.value.length === 1 && currentPage.value > 1) {
        currentPage.value--
      }
    } else {
      alert(result.msg || '删除失败')
    }
  } catch (error) {
    console.error('删除会议失败:', error)
    alert('删除失败，请重试')
  }
}

// 关闭弹窗
const closeMeetingDialog = () => {
  showAddMeetingDialog.value = false
  editingMeeting.value = null
  // 重置表单
  Object.keys(meetingForm).forEach(key => {
    meetingForm[key] = key === 'departmentId' ? 1 : ''
  })
}

onMounted(() => {
  fetchMeetings()
})
</script>

<style scoped>
/* 全局模块容器样式 - 提升整体质感 */
.module-card {
  width: 100%;
  max-width: 1200px;
  margin: 24px auto;
  padding: 28px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  font-family: "Inter", system-ui, sans-serif;
  transition: all 0.3s ease;
}

/* 头部按钮样式 - 突出主操作 */
.module-header {
  margin-bottom: 28px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.btn-primary {
  padding: 12px 24px;
  background: linear-gradient(135deg, #1890ff 0%, #40a9ff 100%);
  color: #ffffff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 15px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn-primary:hover {
  background: linear-gradient(135deg, #40a9ff 0%, #69c0ff 100%);
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(24, 144, 255, 0.25);
}

/* 会议列表与详情容器 - 优化布局比例 */
.meeting-container {
  display: flex;
  gap: 24px;
  min-height: 500px;
  flex-wrap: wrap;
}

/* 会议列表容器样式 */
.meeting-list-container {
  flex: 1;
  min-width: 300px;
  padding: 24px;
  background: #ffffff;
  border: 1px solid #f0f0f0;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.meeting-list-container.list-full {
  flex: 2;
  min-width: 400px;
}

/* 会议列表样式 */
.meeting-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 24px;
}

/* 会议项样式 - 增强交互感 */
.meeting-item {
  padding: 20px;
  background: #ffffff;
  border: 2px solid #f0f0f0;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.meeting-item::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: #1890ff;
  transform: scaleY(0);
  transition: transform 0.3s ease;
}

.meeting-item:hover {
  border-color: #e6f7ff;
  box-shadow: 0 6px 16px rgba(24, 144, 255, 0.12);
  transform: translateY(-2px);
}

.meeting-item:hover::before {
  transform: scaleY(1);
}

.meeting-item.active {
  border-color: #1890ff;
  background: #f0f7ff;
  box-shadow: 0 6px 20px rgba(24, 144, 255, 0.18);
}

.meeting-item.active::before {
  transform: scaleY(1);
}

/* 会议项头部样式 */
.meeting-header h4 {
  margin: 0 0 12px 0;
  font-size: 17px;
  color: #1f2329;
  font-weight: 600;
}

.meeting-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 12px;
  font-size: 14px;
  color: #6b7280;
}

.meeting-meta span {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* 会议项内容样式 */
.meeting-content p {
  margin: 0;
  font-size: 14px;
  color: #4b5563;
  line-height: 1.6;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* 会议详情容器样式 */
.meeting-detail {
  flex: 1;
  min-width: 320px;
  padding: 28px;
  background: #ffffff;
  border: 1px solid #f0f0f0;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  position: relative;
  animation: fadeInUp 0.5s ease;
}

/* 详情头部样式 */
.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 16px;
  margin-bottom: 24px;
  border-bottom: 2px solid #f0f0f0;
}

.detail-header h3 {
  margin: 0;
  font-size: 22px;
  color: #1f2329;
  font-weight: 600;
}

.btn-close {
  width: 40px;
  height: 40px;
  background: #f9fafb;
  border: none;
  border-radius: 50%;
  font-size: 24px;
  cursor: pointer;
  color: #6b7280;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.btn-close:hover {
  background: #f0f0f0;
  color: #1f2329;
  transform: rotate(90deg);
}

/* 详情元数据样式 */
.detail-meta {
  margin-bottom: 28px;
  padding: 20px;
  background: #f9fafb;
  border-radius: 8px;
  border: 1px solid #f0f0f0;
}

.detail-meta p {
  margin: 8px 0;
  font-size: 15px;
  line-height: 1.7;
  color: #4b5563;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
}

.detail-meta p strong {
  color: #1f2329;
  margin-right: 8px;
  white-space: nowrap;
  min-width: 80px;
}

/* 详情内容样式 */
.detail-content {
  margin-bottom: 32px;
}

.detail-content h4 {
  margin: 0 0 16px 0;
  font-size: 16px;
  color: #1f2329;
  font-weight: 600;
}

.detail-content p {
  font-size: 15px;
  color: #4b5563;
  line-height: 1.8;
  white-space: pre-line;
  background: #f9fafb;
  padding: 20px;
  border-radius: 8px;
  border: 1px solid #f0f0f0;
  transition: all 0.3s ease;
}

.detail-content p:hover {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

/* 详情操作按钮样式 */
.detail-actions {
  display: flex;
  gap: 12px;
  padding-top: 24px;
  border-top: 1px solid #f0f0f0;
}

.btn-edit, .btn-delete {
  padding: 9px 18px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-edit {
  background: #e6f7ff;
  color: #1890ff;
}

.btn-edit:hover {
  background: #b3e0ff;
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.2);
}

.btn-delete {
  background: #fff2f0;
  color: #ff4d4f;
}

.btn-delete:hover {
  background: #ffccc7;
  box-shadow: 0 2px 8px rgba(255, 77, 79, 0.2);
}

/* 分页样式 - 优化交互体验 */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  padding-top: 20px;
  margin-top: 8px;
  border-top: 1px solid #f0f0f0;
}

.btn-pagination {
  padding: 8px 16px;
  background: #ffffff;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: #4b5563;
  transition: all 0.3s ease;
}

.btn-pagination:hover:not(:disabled) {
  border-color: #1890ff;
  color: #1890ff;
  background: #f0f7ff;
}

.btn-pagination:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background: #f9fafb;
  border-color: #f0f0f0;
  color: #9ca3af;
}

.page-info {
  font-size: 14px;
  color: #6b7280;
}

/* 弹窗样式 - 增强层次感与动画 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes fadeInUp {
  from { 
    opacity: 0;
    transform: translateY(20px);
  }
  to { 
    opacity: 1;
    transform: translateY(0);
  }
}

.dialog {
  width: 600px;
  max-width: 90vw;
  max-height: 90vh;
  overflow-y: auto;
  background: #ffffff;
  border-radius: 16px;
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.2);
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from { transform: translateY(30px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* 弹窗头部样式 */
.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid #f0f0f0;
}

.dialog-header h3 {
  margin: 0;
  font-size: 18px;
  color: #1f2329;
  font-weight: 600;
}

.dialog-close {
  width: 32px;
  height: 32px;
  background: transparent;
  border: none;
  border-radius: 50%;
  font-size: 20px;
  cursor: pointer;
  color: #6b7280;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.dialog-close:hover {
  background: #f0f0f0;
  color: #1f2329;
}

/* 弹窗内容样式 */
.dialog-content {
  padding: 24px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
  color: #1f2329;
}

.form-group input,
.form-group textarea,
.form-group select {
  width: 100%;
  padding: 11px 14px;
  background: #ffffff;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  box-sizing: border-box;
  font-size: 15px;
  color: #1f2329;
  transition: all 0.3s ease;
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
  outline: none;
  border-color: #1890ff;
  box-shadow: 0 0 0 3px rgba(24, 144, 255, 0.15);
}

.form-group textarea {
  min-height: 120px;
  resize: vertical;
  line-height: 1.6;
}

/* 弹窗底部样式 */
.dialog-footer {
  padding: 16px 24px;
  border-top: 1px solid #f0f0f0;
  text-align: right;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.btn-secondary {
  padding: 9px 18px;
  background: #f9fafb;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: #4b5563;
  transition: all 0.3s ease;
}

.btn-secondary:hover {
  background: #f0f0f0;
  border-color: #d0d0d0;
  color: #1f2329;
}

/* 无数据提示样式 */
.no-data {
  text-align: center;
  padding: 60px 20px;
  color: #9ca3af;
  font-size: 15px;
  background: #f9fafb;
  border-radius: 8px;
  border: 1px dashed #e0e0e0;
}

/* 响应式调整 - 适配移动端 */
@media (max-width: 768px) {
  .module-card {
    padding: 16px;
    margin: 12px auto;
  }

  .meeting-container {
    flex-direction: column;
    gap: 16px;
  }

  .meeting-list-container,
  .meeting-detail {
    padding: 16px;
  }

  .meeting-header h4 {
    font-size: 16px;
  }

  .meeting-meta {
    gap: 12px;
    font-size: 13px;
  }

  .detail-header h3 {
    font-size: 18px;
  }

  .detail-meta p {
    font-size: 14px;
  }

  .detail-content p {
    font-size: 14px;
    padding: 12px;
  }

  .dialog-content {
    padding: 16px;
  }

  .form-group {
    margin-bottom: 16px;
  }
}
</style>